package com.ls.blog.controller;

import com.ls.blog.entity.SysUser;
import com.ls.blog.result.JsonResult;
import com.ls.blog.api.LoginService;
import com.ls.blog.api.SysUserService;
import com.ls.blog.result.ResultCode;
import com.ls.blog.utils.ResultTool;
import com.ls.blog.vo.UserInfo;
import com.ls.blog.vo.UserVo;
import com.qiniu.util.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static java.awt.SystemColor.info;

/**
 * @program: myblog
 * @author: lishun
 * @create: 2021-07-07 16:48
 **/
@Api(tags ={"用户模块"})
@RestController
public class UserController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private SysUserService sysUserService;

    //登录
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public JsonResult login(SysUser user) throws Exception {
        System.out.println(user);
        if (user==null){
            return ResultTool.fail();
        }
        Map<String, Object> map = loginService.login(user);

        return ResultTool.success(ResultCode.LOGIN_SUCCESS,map);

    }

    @ApiOperation(value = "当前用户")
    @GetMapping("/users/currentUser")
    public JsonResult currentUser(@RequestHeader("Authorization") String token) throws Exception {
        UserInfo info = sysUserService.currentUser(token);
        return ResultTool.success();
    }

    /**
     * 用户退出
     * @param
     * @return
     */
    @ApiOperation("登出")
    @GetMapping("/logout")
    public JsonResult logout(){
//        loginService.logout(token);
        return ResultTool.success(ResultCode.REGISTER_SUCCESS);
    }

    /**
     * 注册
     * @return
     */
    @PostMapping("/register")
    public JsonResult register(@RequestBody SysUser sysUser) throws Exception {
        sysUserService.register(sysUser);
        return ResultTool.success();
    }

    /**
     * 分页查询所有用户
     * @return
     */
    @ApiOperation(value = "查询用户")
    @PostMapping("/admin/findUserList")
    public JsonResult<List<UserVo>> findUserList(Long current){
        if (current==null){
            current =1L;
        }
        return ResultTool.success(sysUserService.findUserList(current,7L));
    }

    

}
